<template>
  <div class="app-container">
    <h1>商品列表管理</h1>
    <hr />
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="供应商id" prop="supplier_id">
        <el-input
          v-model="queryParams.supplier_id"
          placeholder="请输入供应商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplier_name" label-width="85px">
        <el-input
          v-model="queryParams.supplier_name"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品id" prop="product_id">
        <el-input
          v-model="queryParams.product_id"
          placeholder="请输入商品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="product_name">
        <el-input
          v-model="queryParams.product_name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进价" prop="purchase_price">
        <el-input
          v-model="queryParams.purchase_price"
          placeholder="请输入进价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="inventory">
        <el-input
          v-model="queryParams.inventory"
          placeholder="请输入库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销量" prop="sales_volume">
        <el-input
          v-model="queryParams.sales_volume"
          placeholder="请输入销量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="production_date">
        <el-input
          v-model="queryParams.production_date"
          placeholder="请输入生产日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-plus"
          size="large"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="filterGoodsList">
      <el-table-column label="图片" prop="img">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row && scope.row.img"
            :src="scope.row.img"
            class="product-image"
            fit="cover"
          />
          <span v-else>无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商id" align="center" prop="supplier_id" />
      <el-table-column label="供应商名称" align="center" prop="supplier_name" />
      <el-table-column label="商品id" align="center" prop="product_id" />
      <el-table-column label="商品名称" align="center" prop="product_name" />
      <el-table-column label="进价" align="center" prop="purchase_price" />
      <el-table-column label="售价" align="center" prop="price" />
      <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="销量" align="center" prop="sales_volume" />
      <el-table-column label="生产日期" align="center" prop="production_date" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.product_id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-show="total > 0"
      :total="total"
      :current-page="queryParams.pageNum"
      :page-size="queryParams.pageSize"
      @current-change="getList"
      layout="prev, pager, next"
    />

    <!-- 添加或修改商品列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供应商id" prop="supplier_id">
          <el-input
            v-model="form.supplier_id"
            placeholder="请输入供应商id"
            disabled
          />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplier_name">
          <el-input
            v-model="form.supplier_name"
            placeholder="请输入供应商名称"
            disabled
          />
        </el-form-item>
        <el-form-item label="商品id" prop="product_id">
          <el-input v-model="form.product_id" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="product_name">
          <el-input v-model="form.product_name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="进价" prop="purchase_price">
          <el-input v-model="form.purchase_price" placeholder="请输入进价" />
        </el-form-item>
        <el-form-item label="售价" prop="price">
          <el-input v-model="form.price" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="库存" prop="inventory">
          <el-input v-model="form.inventory" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="销量" prop="sales_volume">
          <el-input v-model="form.sales_volume" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="生产日期" prop="production_date">
          <el-input
            v-model="form.production_date"
            placeholder="请输入生产日期"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { mapState } from "vuex";

export default {
  name: "TestView",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        img: null,
        supplier_id: null,
        supplier_name: null,
        product_id: null,
        product_name: null,
        purchase_price: null,
        price: null,
        inventory: null,
        sales_volume: null,
        production_date: null,
      },
      // 表单参数
      form: {
        img: null,
        supplier_id: null,
        supplier_name: null,
        product_id: null,
        product_name: null,
        purchase_price: null,
        price: null,
        inventory: null,
        sales_volume: null,
        production_date: null,
      },
      // 表单校验
      rules: {},
      filterGoodsList: [],
    };
  },
  created() {
    this.$store.dispatch("goods/getGoodsList"); // 获取商品列表
    setTimeout(() => {
      this.getList();
      this.loading = false;
    }, 1000);
  },
  computed: {
    // ...mapState({
    //   goodsList: (state) => state.goods.goodsList, // 从 Vuex 中获取商品列表
    // }),
    loginPerson() {
      return (
        this.$store.state.loginPerson ||
        JSON.parse(localStorage.getItem("loginPerson"))
      );
    },
    getGoodsList() {
      return (
        this.$store.state.goodsList ||
        JSON.parse(localStorage.getItem("vuex-goods"))
      );
    },
  },
  methods: {
    /** 查询商品列列表 */
    getList() {
      this.loading = true;
      this.goodsList = this.getGoodsList.filter(
        (item) => item.supplier_id === this.loginPerson.id
      );
      this.filterGoodsList = this.getGoodsList.filter(
        (item) => item.supplier_id === this.loginPerson.id
      );
      this.total = this.goodsList.length;
      this.loading = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        img: null,
        supplier_id: null,
        supplier_name: null,
        product_id: null,
        product_name: null,
        purchase_price: null,
        price: null,
        inventory: null,
        sales_volume: null,
        production_date: null,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.supplier_id !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.supplier_id === this.queryParams.supplier_id
        );
      if (this.queryParams.supplier_name !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.supplier_name === this.queryParams.supplier_name
        );
      if (this.queryParams.product_id !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.product_id === this.queryParams.product_id
        );
      if (this.queryParams.product_name !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.product_name === this.queryParams.product_name
        );
      if (this.queryParams.purchase_price !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.purchase_price === this.queryParams.purchase_price
        );
      if (this.queryParams.price !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.price === this.queryParams.price
        );
      if (this.queryParams.inventory !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.inventory === this.queryParams.inventory
        );
      if (this.queryParams.sales_volume !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.sales_volume === this.queryParams.sales_volume
        );
      if (this.queryParams.production_date !== null)
        this.filterGoodsList = this.filterGoodsList.filter(
          (item) => item.production_date === this.queryParams.production_date
        );
    },
    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        img: null,
        supplier_id: null,
        supplier_name: null,
        product_id: null,
        product_name: null,
        purchase_price: null,
        price: null,
        inventory: null,
        sales_volume: null,
        production_date: null,
      }),
        (this.filterGoodsList = [...this.goodsList]);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
      this.form = {
        ...this.form,
        supplier_id: this.loginPerson.id,
        supplier_name: this.loginPerson.username,
      };
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = { ...row };
      this.open = true;
      this.title = "修改商品";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (
            this.goodsList.find(
              (item) => item.product_id === this.form.product_id
            )
          ) {
            // 更新操作
            this.$store.dispatch("goods/updateProduct", this.form).then(() => {
              this.$message.success("修改成功"); // 修改这里
              this.open = false;
              window.location.reload();
              this.getList();
            });
          } else {
            // 新增操作
            const newProduct = {
              ...this.form,
              img: null,
            };
            this.$store.dispatch("goods/addProduct", newProduct).then(() => {
              this.$message.success("新增成功"); // 修改这里
              this.open = false;
              window.location.reload();
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(productId) {
      this.$confirm(`确认删除商品ID为 ${productId} 的商品？`, "提示", {
        // 修改这里
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch("goods/deleteProduct", productId).then(() => {
            window.location.reload();
            this.getList();
            this.$message.success("删除成功"); // 修改这里
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped></style>
